package com.zbl.zblproject.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.erp.entity.ErpOrder;
import com.zbl.zblproject.erp.entity.ErpTableEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/10
 */
@Repository
public interface ErpOrderMapper extends BaseMapper<ErpOrder> {

    /**
     * 分页功能
     * @param page
     * @param search 查询条件
     * @return
     */
    IPage<ErpOrder> selectPageBySearch(Page page, @Param("search") String search);


    @Select("SELECT SUM(goods_num * goods_price) FROM zbl_erp_orderitem WHERE order_id = #{id}")
    BigDecimal selectOrderTotal(@Param("id") int id);

    /**
     * 获取商品中的第一个商品
     * @param id 订单ID
     * @return 商品ID
     */
    int getGoodsById(int id);

    @Select("SELECT * FROM zbl_erp_order WHERE order_info = #{orderSn}")
    ErpOrder selectErpOrderByOrderSn(@Param("orderSn") String orderSn);

    List<ErpOrder> selectErpOrdersByStatusAndModeAndBrandName(@Param("brandId") int brandId,
                                                              @Param("mode") int mode,
                                                              @Param("status") int status);

    @Select(" SELECT COUNT(*) FROM zbl_erp_order WHERE order_info = #{orderSn} ")
    int selectIsExsitc(@Param("orderSn") String orderSn);


    @Update(" UPDATE zbl_erp_order  SET  status = #{status}  WHERE order_id = #{id} ")
    int updateOrderStatus(@Param("id") int id, @Param("status") int status);

    @Select("SELECT order_info FROM zbl_erp_order WHERE order_id = #{id} ")
    String selectOrderSnById(@Param("id") int id);

    ErpTableEntity selectErpTableEntityByOrderSn(@Param("list") List<String> orderSn);

    @Delete("DELETE FROM zbl_erp_order WHERE order_info = #{orderSn}")
    boolean deleteByOrderSn(@Param("orderSn") String orderSn);
}
